package org.ht.combotable;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JToggleButton;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import org.ht.global.Global;

public class HiddenJToggleButton extends JToggleButton implements MouseListener {

	boolean hiddenState = false;

	Icon falseIcon = new ImageIcon("./resouce/sub.jpg");
	Icon trueIcon = new ImageIcon("./resouce/add.jpg");

	public HiddenJToggleButton() {
		this(false);
	}

	/**
	 * @param icon
	 */

	public HiddenJToggleButton(boolean state) {
		super();
		this.hiddenState = state;
		setIcon(hiddenState ? trueIcon : falseIcon);
		this.setBorderPainted(false);
		this.setBorder(null);
		ChangeListener changeListener = new ChangeListener() {

			@Override
			public void stateChanged(ChangeEvent e) {
				// 改变状态
				hiddenState = !hiddenState;
				refresh();
			}
		};
		this.addChangeListener(changeListener);
	}

	private void refresh() {
		if (hiddenState) {
			setIcon(trueIcon);
		} else {
			setIcon(falseIcon);
		}
		Global.mainpanel.repaint();
		// Global.table.revalidate();
		// Global.table.repaint();
		// Global.table.repaint();
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		fireStateChanged();
	}

	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub

	}

}
